package com.cskaoyan.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author stone
 * @date 2022/04/25 15:54
 */
public class LifeCycleBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean {

    String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        System.out.println("setPassword:" + password);
        this.password = password;
    }

    String beanName;
    BeanFactory beanFactory;
    ApplicationContext applicationContext;

    public LifeCycleBean() {
        System.out.println("Bean的实例化");
    }

    //形参传入的值，就是当前组件在容器中的值
    @Override
    public void setBeanName(String beanName) {
        System.out.println("setBeanName:" + beanName);
        this.beanName = beanName;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setBeanFactory");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("setApplicationContext:" + applicationContext);
        this.applicationContext = applicationContext;
    }

    public void customInit() {
        System.out.println("自定义的init");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet");
    }

    public void customDestroy() {
        System.out.println("自定义的destroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean的destroy");
    }
}
